package com.ricchieri.manage.bean;

import java.util.Map;

import javax.faces.bean.ManagedProperty;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

import com.ricchieri.filter.BasicFilter;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.lazymodel.ScheduleLazyModel;
import com.ricchieri.lazymodel.ScheduleStatusLazyDataModel;
import com.ricchieri.model.Reservation;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IMeanPaymentService;
import com.ricchieri.service.interf.IPassengerTypeService;
import com.ricchieri.service.interf.IReservationService;
import com.ricchieri.service.interf.IReservationTypeService;
import com.ricchieri.service.interf.IScheduleCharterService;
import com.ricchieri.service.interf.ITripService;

public abstract class CommonReservationManageBean extends GenericManageBean {

	private static final long serialVersionUID = -4440280341054520315L;

	@ManagedProperty(value = "#{ReservationTypeService}")
	IReservationTypeService reservationTypeService;
	
	@ManagedProperty(value = "#{PassengerTypeService}")
	IPassengerTypeService passengerTypeService;
	
	@ManagedProperty(value = "#{MeanPaymentService}")
	IMeanPaymentService meanPaymentService;
	
	@ManagedProperty(value = "#{ReservationService}")
	IReservationService reservationService;
	
	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;

	@ManagedProperty(value = "#{ScheduleCharterService}")
	IScheduleCharterService scheduleCharterService;
	
	private String selectedReservationType;
	private String selectedPassengerType;
	private String selectedMeanPayment;
	private Map<String, String> trips = null;
	private Map<String, String> reservationType = null;
	private Map<String, String> passengerType = null;
	private Map<String, String> meanPayment = null;
	
	private ScheduleCharterFilter scheduleCharterFilter = new ScheduleCharterFilter();
	private ScheduleCharter scheduleCharter;
	private ScheduleLazyModel scheduleLazyModel;
	private ScheduleModel eventModel = new DefaultScheduleModel();

	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}

	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public Map<String, String> getTrips() {
		if (trips == null) {
			trips = getTripService().getComboTrips();
		}
	
		return trips;
	}

	public IReservationTypeService getReservationTypeService() {
		return reservationTypeService;
	}

	public void setReservationTypeService(IReservationTypeService reservationTypeService) {
		this.reservationTypeService = reservationTypeService;
	}

	public Map<String, String> getReservationType() {
		if (reservationType==null){
			reservationType=getReservationTypeService().getComboReservationType();
		}
		return reservationType;		
	}

	public void setReservationType(Map<String, String> reservationType) {
		this.reservationType = reservationType;
	}

	public IPassengerTypeService getPassengerTypeService() {
		return passengerTypeService;
	}

	public void setPassengerTypeService(IPassengerTypeService passengerTypeService) {
		this.passengerTypeService = passengerTypeService;
	}

	public Map<String, String> getPassengerType() {
		if (passengerType==null){
			passengerType=getPassengerTypeService().getComboPassengerType();
		}
		return passengerType;		
	}

	public void setPassengerType(Map<String, String> passengerType) {
		this.passengerType = passengerType;
	}

	public String getSelectedPassengerType() {
		return selectedPassengerType;
	}

	public void setSelectedPassengerType(String selectedPassengerType) {
		this.selectedPassengerType = selectedPassengerType;
	}

	public IMeanPaymentService getMeanPaymentService() {
		return meanPaymentService;
	}

	public void setMeanPaymentService(IMeanPaymentService meanPaymentService) {
		this.meanPaymentService = meanPaymentService;
	}

	public Map<String, String> getMeanPayment() {
		if (meanPayment==null){
			meanPayment = getMeanPaymentService().getComboMeanPayment();
		}
		return meanPayment;		
	}

	public void setMeanPayment(Map<String, String> meanPayment) {
		this.meanPayment = meanPayment;
	}

	public String getSelectedMeanPayment() {
		return selectedMeanPayment;
	}

	public void setSelectedMeanPayment(String selectedMeanPayment) {
		this.selectedMeanPayment = selectedMeanPayment;
	}

	public String getSelectedReservationType() {
		return selectedReservationType;
	}

	public void setSelectedReservationType(String selectedReservationType) {
		this.selectedReservationType = selectedReservationType;
	}

	public IReservationService getReservationService() {
		return reservationService;
	}

	public void setReservationService(IReservationService reservationService) {
		this.reservationService = reservationService;
	}
	
	public IScheduleCharterService getScheduleCharterService() {
		return scheduleCharterService;
	}

	public void setScheduleCharterService(
			IScheduleCharterService scheduleCharterService) {
		this.scheduleCharterService = scheduleCharterService;
	}
	
	public ScheduleCharter getScheduleCharter() {
		return scheduleCharter;
	}

	public void setScheduleCharter(ScheduleCharter scheduleCharter) {
		this.scheduleCharter = scheduleCharter;
	}  
	
	public Boolean haveReservation(User user) {
		for (Reservation reservation : getScheduleCharter().getActiveReservations()) {
			if(reservation.getUser().getId() == user.getId()) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}
	
	protected void completeReservationTypes() {
		BasicFilter reservationTypeFilter = new BasicFilter();
		reservationTypeFilter.setId(Integer.valueOf(getSelectedReservationType()));
		getReservation().setReservationType(getReservationTypeService().getReservationTypeById(reservationTypeFilter));
		
		BasicFilter passengerTypeFilter = new BasicFilter();
		passengerTypeFilter.setId(Integer.valueOf(getSelectedPassengerType()));
		getReservation().setPassengerType(getPassengerTypeService().getPassengerTypeById(passengerTypeFilter));
		
		BasicFilter meanPaymentFilter = new BasicFilter();
		meanPaymentFilter.setId(Integer.valueOf(getSelectedMeanPayment()));
		getReservation().setMeanPayment(getMeanPaymentService().getMeanPaymentById(meanPaymentFilter));
	}
	

	public ScheduleCharterFilter getScheduleCharterFilter() {
		return scheduleCharterFilter;
	}

	public void setScheduleCharterFilter(
			ScheduleCharterFilter scheduleCharterFilter) {
		this.scheduleCharterFilter = scheduleCharterFilter;
	}

	public ScheduleLazyModel getScheduleLazyModel() {

		if (scheduleLazyModel == null) {
			scheduleLazyModel = new ScheduleStatusLazyDataModel(
					getScheduleCharterService(), getScheduleCharterFilter(),
					getEventModel());
		}
		return scheduleLazyModel;
	}

	public void setScheduleLazyModel(ScheduleLazyModel scheduleLazyModel) {
		this.scheduleLazyModel = scheduleLazyModel;
	}
	
	public ScheduleModel getEventModel() {
		return eventModel;
	}

	public void setEventModel(ScheduleModel eventModel) {
		this.eventModel = eventModel;
	}

	public void changeTrip() {
		setScheduleLazyModel(null);
		setEventModel(new DefaultScheduleModel());
	}
	
	public abstract String saveReservation();
	protected abstract String getFormName();
	public abstract Reservation getReservation();
	public abstract void onEventSelect(SelectEvent selectEvent);
}